<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Models_Mysql_Translation extends Modules_Core_Models_Translation
{
	protected $_table = 'core_translation';
	
	public function add($translation)
	{
		$this->_conn->insert($translation, $this->_conn->_tablePrefix . $this->_table);
	}
	
	public function update($translation)
	{
		$sql = sprintf("UPDATE " . $this->_conn->_tablePrefix . $this->_table . "
						SET source_item_id = %s, language = '%s', source_language = '%s'
						WHERE item_id = %s AND item_class = '%s'",
						$this->_conn->escape($translation['source_item_id']),
						$this->_conn->escape($translation['language']),
						$this->_conn->escape((null == $translation['source_language']) ? 'null' : $translation['source_language']),
						$this->_conn->escape($translation['item_id']),
						$this->_conn->escape($translation['item_class']));
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function delete($id, $class)
	{
		$sql = sprintf("UPDATE " . $this->_conn->_tablePrefix . $this->_table . "
						SET source_item_id = item_id, source_language = NULL
						WHERE source_item_id = %s AND item_class = '%s'",
						$this->_conn->escape($id),
						$this->_conn->escape($class));
		$this->_conn->query($sql);
		
		$sql = sprintf("DELETE FROM " . $this->_conn->_tablePrefix . $this->_table . "
						WHERE item_id = %s AND item_class = '%s'",
						$this->_conn->escape($id),
						$this->_conn->escape($class));
		$this->_conn->query($sql);
		return $this->_conn->affectedRows();
	}
	
	public function getItems($id, $class, $lang = null)
	{
		$sql = sprintf("SELECT tr1.*
						FROM " . $this->_conn->_tablePrefix . $this->_table . " AS tr1
						INNER JOIN " . $this->_conn->_tablePrefix . $this->_table . " AS tr2
							ON (tr1.item_id = '%s' AND tr1.source_item_id = tr2.item_id)
							OR (tr2.item_id = '%s' AND tr1.item_id = tr2.source_item_id)
							OR (tr1.source_item_id = '%s' AND tr1.source_item_id = tr2.source_item_id)
						WHERE tr1.item_class = '%s'",
						$this->_conn->escape($id),
						$this->_conn->escape($id),
						$this->_conn->escape($id),
						$this->_conn->escape($class));
						
		if ($lang != null) {
			$sql .= sprintf(" AND tr1.language = '%s'", $this->_conn->escape($lang));
		}
		$sql .= sprintf(" AND tr2.item_class = '%s'
						GROUP BY tr1.translation_id",
						$this->_conn->escape($class));
		$rs   = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		$this->_conn->freeResult($rs);
		return $rows;
	}
}